package cn.com.franke;

import net.sf.json.JSONObject;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.junit.Test;
import cn.com.franke.util.FastJsonUtil;
import cn.com.franke.util.JSONUtil;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 测试总结：
 * 把数据转化为json对象：fastJson会把value为null的数据舍弃
 * 解析jsonStr为json对象：
        原生json可能会有精度损失(get,getString)
        原生json可能会把value为null的数据解析成null字符串（getString方法）
 *
 * Created by liuxianzhao on 2017/4/12.
 */
public class TestJson {


//    @Test
//    public void test1() {
//        cn.com.franke.DemoBean bean1 = new cn.com.franke.DemoBean("10.2121212", 1);
//        cn.com.franke.DemoBean bean2 = new cn.com.franke.DemoBean("11.2121212", 2);
//        cn.com.franke.DemoBean bean3 = new cn.com.franke.DemoBean("12.2121212", 3);
//        List<cn.com.franke.DemoBean> list = new ArrayList<cn.com.franke.DemoBean>();
//        list.add(bean1);
//        list.add(bean2);
//        list.add(bean3);
//        String jsonArrStr = JSONArray.fromObject(list).toString();
//        JSONArray jsonArray = JSONArray.fromObject(jsonArrStr);
//        JSONObject o = (JSONObject) jsonArray.get(0);
//        System.out.println(o.get("money"));
//        System.out.println(o.getString("money"));
//    }
@Test
public void test1() {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("money", 10.2121212);  //TODO 转json不能这么放
    map.put("moneyStr", "10.2121212");
    map.put("moneyNull", null);

    String str = JSONObject.fromObject(map).toString();
    JSONObject object = JSONObject.fromObject(str);
    System.out.println(object.get("money"));   //精度损失
    System.out.println(object.getString("money"));          //精度损失
    System.out.println(new BigDecimal(object.getString("money")));        //精度损失
    System.out.println(object.get("moneyStr"));
    System.out.println(object.getString("moneyStr"));
    System.out.println(new BigDecimal(object.getString("moneyStr")));
    System.out.println("null".equals(object.get("moneyNull")));    //  false
    System.out.println("null".equals(object.getString("moneyNull")));     //  true
    System.out.println("null".equals(object.get("a")));                  //  false
    System.out.println("null".equals(object.getString("a")));         //  JSONException

}
    @Test
    public void test2() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("money", 10.2121212);  //TODO 转json不能这么放
        map.put("moneyStr", "10.2121212");
        map.put("moneyNull", null);

        JSONObject object =JSONObject.fromObject(map);
        System.out.println(object.get("money"));   //精度损失
        System.out.println(object.getString("money"));          //精度损失
        System.out.println(new BigDecimal(object.getString("money")));        //精度损失
        System.out.println(object.get("moneyStr"));
        System.out.println(object.getString("moneyStr"));
        System.out.println(new BigDecimal(object.getString("moneyStr")));
        System.out.println("null".equals(object.get("moneyNull")));    //  false
        System.out.println("null".equals(object.getString("moneyNull")));     //  true
        System.out.println("null".equals(object.get("a")));                  //  false
        System.out.println("null".equals(object.getString("a")));         //  JSONException

    }

    @Test
    public void testFastJson1() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("money", 10.2121212);
        map.put("moneyStr", "10.2121212");
        map.put("moneyNull", null);
        String str = FastJsonUtil.toJSONStr(map);
        com.alibaba.fastjson.JSONObject object = FastJsonUtil.parseToJSONObejct(str);
        System.out.println(object.get("money"));
        System.out.println(object.getString("money"));
        System.out.println(new BigDecimal(object.getString("money")));
        System.out.println(object.get("moneyStr"));
        System.out.println(object.getString("moneyStr"));
        System.out.println(new BigDecimal(object.getString("moneyStr")));
        System.out.println("null".equals(object.get("moneyNull")));
        System.out.println("null".equals(object.getString("moneyNull")));
        System.out.println("null".equals(object.get("a")));
        System.out.println("null".equals(object.getString("a")));
    }
    @Test
    public void testFastJson2() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("money", 10.2121212);
        map.put("moneyStr", "10.2121212");
        map.put("moneyNull", null);
        String str = FastJsonUtil.toJSONStr(map);
        JSONObject object = JSONObject.fromObject(str);
        System.out.println(object.get("money"));
        System.out.println(object.getString("money"));
        System.out.println(new BigDecimal(object.getString("money")));
        System.out.println(object.get("moneyStr"));
        System.out.println(object.getString("moneyStr"));
        System.out.println(new BigDecimal(object.getString("moneyStr")));
        System.out.println("null".equals(object.get("moneyNull")));
        System.out.println("null".equals(object.getString("moneyNull")));
        System.out.println("null".equals(object.get("a")));
        System.out.println("null".equals(object.getString("a")));
    }
    //测试结果：没有损失精度
    @Test
    public void testJackSon2() {
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("money", 10.2121212);
            map.put("moneyStr", "10.2121212");
            map.put("moneyNull", null);
            String str = JSONUtil.writeValueAsString(map);
            Map object = JSONUtil.toObject(str, Map.class);
            System.out.println(object.get("money"));
            System.out.println(object.get("moneyStr"));
            System.out.println("null".equals((String) object.get("moneyNull")));
            System.out.println(new BigDecimal(object.get("money").toString()));
            System.out.println(new BigDecimal((String) object.get("moneyStr")));
            System.out.println("null".equals((String) object.get("a")));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

